﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Forest.Cat.Domain.Services.JobHunting;
using Forest.Cat.Domain;
using Forest.Cat.Domain.Entities;
using Forest.Cat.Domain.Services.Catalog;
using Forest.Cat.Web.Models.Catalog;
using Forest.Cat.Domain.Configuration;
using Forest.Cat.Domain.Services.Compnaies;
using Forest.Cat.Web.Models.JobHunting;

namespace Forest.Cat.Web.Controllers
{
    public class JobHuntingController : Controller
    {
        #region Fields
        private readonly ISavedJobService _savedJobService;
        private readonly ISavedCompanyService _savedCompanyService;
        private readonly IJobPositionService _jobPositionService;
        private readonly ICompanyService _companyService;
        private readonly ILovService _lovService;
        private readonly IWorkContext _workContext;
        #endregion

        #region Constructures
        public JobHuntingController(ISavedJobService savedJobService,
            ISavedCompanyService savedCompanyService,
            ICompanyService companyService,
            IJobPositionService jobPositionService,
            ILovService lovService,
            IWorkContext workContext)
        {
            _savedJobService = savedJobService;
            _savedCompanyService = savedCompanyService;
            _jobPositionService = jobPositionService;
            _companyService = companyService;
            _lovService = lovService;
            _workContext = workContext;
        }
        #endregion

        #region Methods
        #region 关注企业

        /// <summary>
        /// 关注企业
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult SavedCompanyView(Guid id)
        {
            bool isSaved = false;
            if (Request.IsAuthenticated)
            {
                //是否已收藏
                isSaved = _savedCompanyService.GetSavedCompanysByJobseekerId(_workContext.CurrentUser.Id).Any(x => x.CompanyId.Equals(id));
            }
            return PartialView(new SavedCompanyViewModel()
            {
                IsSaved = isSaved,
                CompanyId = id
            });
        }

        /// <summary>
        /// 关注企业
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DoSavedCompany(Guid id, bool isDoSave)
        {
            //如果是关注
            if (isDoSave)
            {
                var company = _companyService.GetCompanyById(id);
                var savedCompany = new SavedCompany()
                {
                    CompanyId = id,
                    CompanyName = company.CompanyName,
                    CompanyType = _lovService.GetLovName(company.CompanyType.Value),
                    DateSaved = DateTime.Now,
                    Employees = company.Employees.Value,
                    Id = Guid.NewGuid(),
                    Industry = _lovService.GetLovName(company.Industry.Value),
                    JobseekerId = _workContext.CurrentUser.Id,
                    IsSubscribed = false
                };
                _savedCompanyService.InsertSavedCompany(savedCompany);
            }
            //如果是取消关注
            else
            {
                var savedCompany = _savedCompanyService.GetSavedCompanysByJobseekerId(_workContext.CurrentUser.Id).FirstOrDefault(x => x.CompanyId.Equals(id));
                if (savedCompany != null)
                {
                    _savedCompanyService.DeleteSavedCompany(savedCompany);
                }
            }
            return PartialView("SavedCompanyView", new SavedCompanyViewModel()
            {
                IsSaved = isDoSave,
                CompanyId = id
            });
        }
        #endregion

        #region 收藏职位
        /// <summary>
        /// 收藏职位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult SavedJobView(Guid id)
        {
            bool isSaved = false;
            if (Request.IsAuthenticated)
            {
                //是否已收藏
                isSaved = _savedJobService.GetSavedJobsByJobseekerId(_workContext.CurrentUser.Id).Any(x => x.JobPositionId.Equals(id));
            }
            return PartialView(new SavedJobViewModel()
            {
                IsSaved = isSaved,
                JobId = id
            });
        }

        /// <summary>
        /// 收藏职位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DoSavedJob(Guid id, bool isDoSave)
        {
            if (isDoSave)
            {
                var job = _jobPositionService.GetJobPositionById(id);
                var company = _companyService.GetCompanyById(job.CompanyId.Value);
                var savedJob = new SavedJob()
                {
                    CompanyId = company.Id,
                    CompanyName = company.CompanyName,
                    DateSaved = DateTime.Now,
                    EndDate = job.EndDate,
                    Id = Guid.NewGuid(),
                    JobPositionId = id,
                    JobseekerId = _workContext.CurrentUser.Id,
                    JobTitle = job.JobTitle,
                    Location = _lovService.GetLovName(job.Province) + _lovService.GetLovName(job.City),
                    StartDate = job.StartDate.Value,
                };
                _savedJobService.InsertSavedJob(savedJob);
            }
            else 
            {
                var savedJob = _savedJobService.GetSavedJobsByJobseekerId(_workContext.CurrentUser.Id).FirstOrDefault(x => x.JobPositionId.Equals(id));
                if (savedJob != null)
                {
                    _savedJobService.DeleteSavedJob(savedJob);
                }
            }

            return PartialView("SavedJobView", new SavedJobViewModel()
            {
                IsSaved = isDoSave,
                JobId = id
            });
        }


        #endregion

        #endregion
    }
}
